basket.require({
	url : 'http://cdn.bootcss.com/jquery/2.1.4/jquery.min.js'
}, {
	url : "http://cdn.bootcss.com/angular.js/1.4.3/angular.min.js"
}, {
	url : "http://cdn.bootcss.com/alertify.js/0.3.11/alertify.min.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/persistence.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/timer.js"
}, {
	url : "http://cdn.bootcss.com/async/1.4.0/async.min.js"
}, {
	url : settings.getIp() + "/peiwan-server/static/js/spark-md5.min.js"
}).then(function() {
	basket.require({
		url : settings.getIp() + "/peiwan-server/static/js/upyun-mu.min.js"
	}).then(ready);
});

function controller(s) {
	var curKey = null;
	var curId = null;
	var ACTION_INSTERT = 0;
	var ACTION_UPDATE = 1;
	var ACTION_ADD_PHOTO = 2;
	var ACTION_UPDATE_PHOTO = 3;
	var action = -1;// insert
	var offset = 0;
	var LIMIT = 20;
	if (!persistence.get("session")) {
		location.href = "admin_login.html";
		return;
	}
	var queryList = function() {
		ajax("/findNetbarLikeName", {
			name : $("#netbar_name_text").val(),
			offset : offset,
			limit : LIMIT
		}, function(data) {
			console.log(data)
			var json = JSON.parse(data);
			if (json.status == 1) {
				location.href = "admin_login.html";
				return;
			}
			var netbars = json.entity;
			s.$apply(function() {
				s.netbars = netbars;
			});
		});
	}
	s.startSearch = function() {
		queryList();
	};
	s.showInsertNetbarForm = function() {
		document.querySelector('#insert_netbar_dialog').showModal();
	};
	s.closeDialog = function() {
		$("#netbar_insert_name").val("");
		$("#netbar_insert_sort").val("");
		$("#netbar_insert_phone").val("");
		$("#netbar_insert_address").val("");
		document.querySelector('#insert_netbar_dialog').close();
	};
	s.ok = function() {
		action = ACTION_INSTERT;
		uploadFile(new Date().getTime() + "_" + randomStr(128), "#file");
	}
	s.lookPhotos = function(netbar) {
		s.netbar = netbar;
		curId = netbar.id;
		document.querySelector("#photo_wall_dialog").showModal();
	}
	s.addPhoto = function(netbar) {
		action = ACTION_ADD_PHOTO;
		uploadFile(new Date().getTime() + "_" + randomStr(128), "#photo_file");
	}
	s.changeNetbarPhoto = function(netbarPhotoId) {
		action = ACTION_UPDATE_PHOTO;
		s.netbarPhotoId = netbarPhotoId;
		uploadFile(new Date().getTime() + "_" + randomStr(128), "#photo_file");
	}
	s.deleteNetbarPhoto = function(netbarPhotoId) {
		ajax("/deleteNetbarPhoto", {
			session : persistence.get("session"),
			id : netbarPhotoId
		}, function(data) {
			console.log(data)
			var json = JSON.parse(data);
			if (json.status == 0) {
				alertify.success("操作成功");
				queryList();
			} else if (json.status == 1) {
				location.href = "admin_login.html";
				return;
			} else {
				alertify.error("操作失败");
			}
			document.querySelector('#photo_wall_dialog').close();
		});
	}
	s.updateOk = function() {
		var params = {};
		params[curKey] = $("#netbar_update_text").val();
		params.session = persistence.get("session");
		params.id = curId;
		ajax("/updateNetbar", params, function(data) {
			console.log(data)
			var json = JSON.parse(data);
			if (json.status == 0) {
				alertify.success("更新成功");
				queryList();
			} else if (json.status == 1) {
				location.href = "admin_login.html";
				return;
			} else {
				alertify.error("更新失败");
			}
			document.querySelector('#update_netbar_dialog').close()
		});
	}
	s.updateOk2 = function() {
		action = ACTION_UPDATE;
		uploadFile(new Date().getTime() + "_" + randomStr(128),
				"#netbar_update_file");
	}
	s.updateNetbar = function(id, key, value) {
		curId = id;
		curKey = key;
		if (key == 'name') {
			s.label = "名称";
			s.hint = "名称";
			$("#netbar_update_text").val(value);
			document.querySelector('#update_netbar_dialog').showModal();
		} else if (key == 'sort') {
			s.label = "排序";
			s.hint = "排序";
			$("#netbar_update_text").val(value);
			document.querySelector('#update_netbar_dialog').showModal();
		} else if (key == 'address') {
			s.label = "地址";
			s.hint = "地址";
			$("#netbar_update_text").val(value);
			document.querySelector('#update_netbar_dialog').showModal();
		} else if (key == 'phone') {
			s.label = "电话";
			s.hint = "电话";
			$("#netbar_update_text").val(value);
			document.querySelector('#update_netbar_dialog').showModal();
		} else if (key == 'photoUrl') {
			s.imgUrl = value;
			document.querySelector('#update_netbar_dialog2').showModal();
		}
	}
	s.closePhotoWallDialog = function() {
		document.querySelector('#photo_wall_dialog').close();
	}

	s.updateCancel = function() {
		$("#netbar_update_text").val("");
		document.querySelector('#update_netbar_dialog').close();
	}
	s.updateCancel2 = function() {
		document.querySelector('#update_netbar_dialog2').close();
	}
	s.deleteNetbar = function(id) {
		if (confirm("确认删除？")) {
//			ajax("/deleteNetbar", {
//				session : persistence.get("session"),
//				id : id
//			}, function(data) {
//				console.log(data)
//				var json = JSON.parse(data);
//				if (json.status == 1) {
//					location.href = "admin_login.html";
//					return;
//				}
//				queryList();
//				alertify.success("更新成功");
//			});
		}
	}
	document.addEventListener('uploaded', function(e) {
		var photoUrl = settings.getImgLinkUrl() + e.detail.path;
		if (action == ACTION_INSTERT) {
			var name = $("#netbar_insert_name").val();
			var sort = $("#netbar_insert_sort").val();
			var phone = $("#netbar_insert_phone").val();
			var address = $("#netbar_insert_address").val();
			ajax("/insertNetbar", {
				session : persistence.get("session"),
				name : name,
				sort : sort,
				photoUrl : photoUrl,
				phone : phone,
				address : address
			}, function(data) {
				console.log(data);
				s.closeDialog();
				queryList();
			});
		} else if (action == ACTION_UPDATE) {
			var params = {};
			params.photoUrl = photoUrl;
			params.session = persistence.get("session");
			params.id = curId;
			ajax("/updateNetbar", params, function(data) {
				console.log(data)
				var json = JSON.parse(data);
				if (json.status == 0) {
					alertify.success("更新成功");
					queryList();
				} else if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				} else {
					alertify.error("更新失败");
				}
				document.querySelector('#update_netbar_dialog2').close()
			});
		} else if (action == ACTION_ADD_PHOTO) {
			ajax("/addNetbarPhoto", {
				session : persistence.get("session"),
				netbarId : s.netbar.id,
				url : photoUrl
			}, function(data) {
				console.log(data)
				var json = JSON.parse(data);
				if (json.status == 0) {
					alertify.success("添加成功");
					queryList();
				} else if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				} else {
					alertify.error("添加失败");
				}
				document.querySelector('#photo_wall_dialog').close();
			});
		} else if (action == ACTION_UPDATE_PHOTO) {
			ajax("/updateNetbarPhoto", {
				session : persistence.get("session"),
				id : s.netbarPhotoId,
				url : photoUrl
			}, function(data) {
				console.log(data)
				var json = JSON.parse(data);
				if (json.status == 0) {
					alertify.success("更新成功");
					queryList();
				} else if (json.status == 1) {
					location.href = "admin_login.html";
					return;
				} else {
					alertify.error("更新失败");
				}
			});
		}

	});

}

function ready() {
	persistence.setNamespace("peiwan_admin");
	var peiwanAdmin = angular.module('peiwan_admin', []);
	peiwanAdmin.controller('NetbarListCtrl', [ "$scope", controller ]);
}
